package handler

import (
	"blog/api/v1"
	"blog/internal/service"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type UserHandler struct {
	*Handler
	userService service.UserService
}

func NewUserHandler(handler *Handler, userService service.UserService) *UserHandler {
	return &UserHandler{
		Handler:     handler,
		userService: userService,
	}
}

// 注册
func (h *UserHandler) Register(ctx *gin.Context) {
	req := new(v1.RegisterUserReq)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, err, nil)
		return
	}

	user, err := h.userService.Register(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusConflict, err, nil)
		return
	}

	resp := &v1.UserInfo{
		ID:           user.ID,
		UserName:     user.UserName,
		Type:         user.Type,
		HeadPortrait: user.HeadPortraitURL,
	}

	v1.HandleSuccess(ctx, resp)
}

// 登录
func (h *UserHandler) Login(ctx *gin.Context) {
	req := new(v1.LoginReq)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, err, nil)
		return
	}

	user, token, err := h.userService.Login(ctx, req.UserName, req.Password)
	if err != nil {
		fmt.Println(err)
		v1.HandleError(ctx, 400, err, nil)
		return
	}

	resp := &v1.LoginResponse{
		User: v1.UserInfo{
			ID:           user.ID,
			UserName:     user.UserName,
			Type:         user.Type,
			HeadPortrait: user.HeadPortraitURL,
		},
		Token: token,
	}

	v1.HandleSuccess(ctx, resp)
}

// 获取所有用户
func (h *UserHandler) GetAllUsers(ctx *gin.Context) {
	id := GetUserIdFromCtx(ctx)
	usertype := GetUserTypeFromCtx(ctx)
	fmt.Println(id)
	fmt.Println(usertype)
	users, err := h.userService.GetAllUsers(ctx)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, users)
}

func (h *UserHandler) ResetPassword(ctx *gin.Context) {
	userIDStr := ctx.Param("id")
	userId, err := strconv.ParseUint(userIDStr, 10, 64)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, errors.New("非法用户 ID"), nil)
		return
	}

	if err := h.userService.ResetPassword(ctx, uint(userId)); err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

// 删除用户
func (h *UserHandler) DeleteUser(ctx *gin.Context) {
	userIDStr := ctx.Param("id")
	userId, err := strconv.ParseUint(userIDStr, 10, 64)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, err, nil)
		return
	}
	if err := h.userService.DeleteUser(ctx, uint(userId)); err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

// 修改用户信息
func (h *UserHandler) UpdateUserInfo(ctx *gin.Context) {
	req := new(v1.UpdateUserReq)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, err, nil)
		return
	}
	if err := h.userService.UpdateUserInfo(ctx, req); err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

// 升级用户权限
func (h *UserHandler) UpgradeUserToCreator(ctx *gin.Context) {
	userIDStr := ctx.Param("id")
	userId, err := strconv.ParseUint(userIDStr, 10, 64)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, err, nil)
		return
	}
	if err := h.userService.UpgradeUserTypeToCreator(ctx, uint(userId)); err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, "已升级为创作者权限")
}
